package com.example.dell.newstart.home;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.newstart.R;
import com.example.dell.newstart.base.NoScrollGridView;
import com.example.dell.newstart.base.RequestInterface;
import com.example.dell.newstart.bean.Product;
import com.example.dell.newstart.detail.Goods_Activity;

import java.util.List;


public class GoodsAdapter extends BaseAdapter {
    private static final String TAG = "GoodsAdapter";
    private List<Product> productList;
    private Context context;
    private Product product;


    public GoodsAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;

    }

    //返回数据的条数
    @Override
    public int getCount() {

        return productList.size() == 0 ? 2 : productList.size();
    }

    @Override
    public Object getItem(int position) {
        return productList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_home_product, null);
            viewHolder.imageView = convertView.findViewById(R.id.iv_type_protrait);
           viewHolder.textView = convertView.findViewById(R.id.tv_type_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            //viewHolder.imageView.setImageResource(appliancePictures[position%4]);
            /*           ViewGroup.LayoutParams params = convertView.getLayoutParams();
             *//*      Log.d(TAG, "getView: params " + params);
            GetPhoneProperty getPhoneProperty = new GetPhoneProperty(context);
            int allSpace = getPhoneProperty.dp2px(8 )*5;
            int screenWidth=getPhoneProperty.getPhoneWidthPix();
            Log.d(TAG, "getView: width "+screenWidth+" allSpace "+allSpace);
            int calculateWidth = (getPhoneProperty.getPhoneWidthPix() - allSpace) / 4;*//*
            params.height = params.height;
            view.setLayoutParams(params);*/
        }
        if (((NoScrollGridView) parent).isOnMeasure) {
            return convertView;
        }
        if (productList != null && productList.size() != 0) {
            product = productList.get(position);
            //设置名称
            viewHolder.textView.setText(product.getName());
            //加载图片
            Glide.with(context)
                    .load(RequestInterface.baseImgPath + product.getMainImage())
                    .into(viewHolder.imageView);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    product = productList.get(position);
                    Intent intent = new Intent(context, Goods_Activity.class);
                    //把商品信息从intent传递到 ProductDetailActivity 活动
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("product", product);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
        }


        return convertView;
    }

    class ViewHolder {
        private ImageView imageView;
        private TextView textView;

    }
}
